package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务实现类
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 条件分页查询用户列表
     */
    @Override
    public PageResult<User> getUsers(UserDto userDto) {
        // 开始分页
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        // 执行查询
        List<User> users = userMapper.getUsers(userDto);
        // 获取分页信息
        Page<User> pageInfo = (Page<User>) users;
        // 封装成 PageResult 对象
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }
    @Override
    public void addUser(User user) {
        //设置默认密码
        user.setPassword(user.getUsername()+"123");
        //密码加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //添加创建时间
        user.setCreateTime(LocalDateTime.now());
        //添加更新时间
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insert(user);
    }
//删除用户
    @Override
    public void deleteUser(List<Integer> ids) {
        userMapper.deleteUser(ids);
    }
//修改用户
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }
    // 根据ID查询用户信息
    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }
//根据角色标签查询用户
    @Override
    public List<User> getByRoleLabel(String roleLabel) {
        return userMapper.getByRoleLabel(roleLabel);
    }
    //用户登录
    @Override
    public LoginResultVo login(String username, String password) {
        //调用mapper查询用户信息
        User user = userMapper.selectByUsername(username);
        //判断用户或者密码
        if (user == null || !user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
            throw new BusinessException("用户名或密码错误");
        }
        //判断用户状态
        if (user.getStatus() == 0) {
            throw new BusinessException("用户已停用");
        }
        //构造登录结果
        LoginResultVo loginResultVo =new LoginResultVo();
        loginResultVo.setId(user.getId());
        loginResultVo.setUsername(user.getUsername());
        loginResultVo.setName(user.getName());
        loginResultVo.setImage(user.getImage());
        loginResultVo.setRoleLabel(user.getRoleLabel());

        //生成令牌[定义这个claims对象，不太理解，以及对象的类型]【令牌生成逻辑不太清楚】
        Map<String ,Object> claims=new HashMap<>();
        claims.put("id",user.getId());
        claims.put("username",user.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        //返回结果
        return loginResultVo;


    }
}
